﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using TowerDef_s_enseLibrary.map;
using TowerDef_s_enseLibrary.enemy;
using TowerDef_s_enseLibrary.player;

namespace TowerDef_s_enseLibrary.game
{
    public class WaveManager
    {
        #region variables

        private int _numberOfWaves; // How many _waves the game will have

        private float _timeSinceLastWave; // How long since the last wave ended

        private bool _waveFinished = false; // Is the current wave over?

        private Queue<Wave> _waves = new Queue<Wave>(); // A queue of all our _waves

        private Texture2D _enemyTexture; // The Texture used to draw the _enemies

        private Map _map; // A reference to our _map class

        /// <summary>
        /// Get current wave
        /// </summary>
        public Wave CurrentWave // Get the wave at the front of the queue
        {
            get
            {
                return _waves.Peek();
            }
        }

        /// <summary>
        /// Get current enemie list
        /// </summary>
        public List<Enemy> Enemies // Get a list of the current enemeies
        {
            get
            {
                return CurrentWave.Enemies;
            }
        }

        /// <summary>
        /// Get current round number
        /// </summary>
        public int Round // Returns the wave number
        {
            get
            {
                return CurrentWave.RoundNumber + 1;
            }
        }

        #endregion variables

        #region functions

        /// <summary>
        /// Creates the wave manager
        /// </summary>
        /// <param name="player">The playing player</param>
        /// <param name="map">The map</param>
        /// <param name="numberOfWaves">The number of _waves</param>
        /// <param name="enemyTexture">The Texture fpr the _enemies</param>
        /// <param name="healthTexture">The healthbar Texture</param>
        public WaveManager(Player player, Map map, int numberOfWaves, Texture2D enemyTexture, Texture2D healthTexture)
        {
            this._numberOfWaves = numberOfWaves;
            this._enemyTexture = enemyTexture;

            this._map = map;

            for(int i = 0; i < numberOfWaves; i++)
            {
                int initialNumerOfEnemies = 6;
                int numberModifier = (i / 6) + 1;

                // Pass the wave class our health Texture.
                Wave wave = new Wave(i, initialNumerOfEnemies * numberModifier,
                    player, map, enemyTexture, healthTexture);
                _waves.Enqueue(wave);
            }
            StartNextWave();
        }

        /// <summary>
        /// Starts the next wave 
        /// </summary>
        private void StartNextWave()
        {
            if(_waves.Count > 0) // If there are still _waves left
            {
                _waves.Peek().Start(); // Start the next one
                _timeSinceLastWave = 0; // Reset timer
                _waveFinished = false;
            }
        }

        /// <summary>
        /// Updates the wave manager
        /// </summary>
        /// <param name="gameTime">The current game time</param>
        public void Update(GameTime gameTime)
        {
            CurrentWave.Update(gameTime); // Update the wave

            if(CurrentWave.RoundOver) // Check if it has finished
            {
                _waveFinished = true;
            }

            if(_waveFinished) // If it has finished
            {
                _timeSinceLastWave += (float)gameTime.ElapsedGameTime.TotalSeconds; // Start the timer
            }

            if(_timeSinceLastWave > 5.0f) // If 30 seconds has passed
            {
                _waves.Dequeue(); // Remove the finished wave
                StartNextWave(); // Start the next wave
            }
        }

        /// <summary>
        /// Draws the current wave textures
        /// </summary>
        /// <param name="spriteBatch">A group of sprite settings</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            CurrentWave.Draw(spriteBatch);
        }

        #endregion functions

        #region events



        #endregion events
    }
}
